package com.epam.command.logic;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;

import com.epam.command.admin.CreateHotelCommand;
import com.epam.command.admin.CreateTouristVoucherCommand;
import com.epam.command.admin.GoToPageCommand;
import com.epam.command.admin.UpdateLastMinuteCommand;
import com.epam.command.admin.UpdateUserCommand;
import com.epam.command.load.LoadAllHotelsCommand;
import com.epam.command.load.LoadAllUsersCommand;
import com.epam.command.load.LoadAllVouchersCommand;
import com.epam.command.load.LoadPurchasesCommand;
import com.epam.command.load.LoadToursCommand;
import com.epam.command.load.LoadUserCommand;
import com.epam.command.login.LoginCommand;
import com.epam.command.login.RegisterCommand;
import com.epam.command.login.SignOutCommand;
import com.epam.command.user.AddToCartCommand;
import com.epam.command.user.BuyCommand;
import com.epam.command.user.RemoveFromCartCommand;

public enum CommandFactory {
	INSTANCE;
	
	private static final Logger LOG = Logger.getLogger(CommandFactory.class);
	
	private Map<String, Command> commands = new HashMap<String, Command>();
		
	public Command getCommand(HttpServletRequest request) {
		
		if (commands.size() == 0) {
			addCommands();
		}
		
		String action = request.getParameter("action");
		
		Command command = commands.get(action);
		LOG.info(command + " executed");
		return command;
	}
	
	private void addCommands() {
		commands.put("login", new LoginCommand());
		commands.put("register", new RegisterCommand());
		commands.put("loadPurchases", new LoadPurchasesCommand());
		commands.put("loadTours", new LoadToursCommand());
		commands.put("buy", new BuyCommand());
		commands.put("signOut", new SignOutCommand());
		commands.put("addToCart", new AddToCartCommand());
		commands.put("removeFromCart", new RemoveFromCartCommand());
		
				
		commands.put("createHotel", new CreateHotelCommand());
		commands.put("createTouristVoucher", new CreateTouristVoucherCommand());
		commands.put("updateLastMinute", new UpdateLastMinuteCommand());
		commands.put("updateUser", new UpdateUserCommand());
		commands.put("goToPage", new GoToPageCommand());
		commands.put("loadUser", new LoadUserCommand());
		commands.put("loadAllUsers", new LoadAllUsersCommand());
		commands.put("loadAllVouchers", new LoadAllVouchersCommand());
		commands.put("loadAllHotels", new LoadAllHotelsCommand());
	}
	
}
